﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Ue.Core.FileSystems;

namespace Ue.Framework.Tests.Core.FileSystems
{
    /// <summary>
    /// Folder 的摘要说明
    /// </summary>
    [TestClass]
    public class FolderTests
    {
        private IFolder folder;
        private IFolderRoot folderRoot;

        [TestInitialize]
        public void Initial()
        {
            this.folderRoot = new FolderRoot("Directory1");
            this.folder = new Folder(this.folderRoot);

            Directory.CreateDirectory("Directory1");
        }

        [TestMethod]
        public void RootIsNullThenThrowArgumentNullException()
        {
            try
            {
                this.folder = new Folder(null);
                Assert.Fail("失败，当参数root为null时，应该抛出ArgumentNullException异常");
            }
            catch(ArgumentNullException){
                
            }
        }

        [TestMethod]
        public void Combine()
        {
            Assert.AreEqual("", this.folder.Combine(""));

            Assert.AreEqual("", this.folder.Combine(null));

            Assert.AreEqual("ChildDirectory1", this.folder.Combine("ChildDirectory1"));

            Assert.AreEqual("/ChildDirectory1", this.folder.Combine("/ChildDirectory1"));

            Assert.AreEqual("ChildDirectory1/ChildDirectory2", this.folder.Combine("ChildDirectory1", "ChildDirectory2"));
            
            Assert.AreEqual("/ChildDirectory1/ChildDirectory2", this.folder.Combine("/ChildDirectory1", "ChildDirectory2"));
            
            Assert.AreEqual("ChildDirectory1/ChildDirectory1/ChildDirectory2", this.folder.Combine("ChildDirectory1", "ChildDirectory1/ChildDirectory2"));
            
            Assert.AreEqual("/ChildDirectory1/ChildDirectory2", this.folder.Combine("ChildDirectory1", "/ChildDirectory1/ChildDirectory2"));
        }

        [TestMethod]
        [DeploymentItem("Core/FileSystems/MockFiles", "Directory1/MockFiles")]
        public void GetDirectories()
        {
            var directories = this.folder.GetDirectories("NoDirectory");
            Assert.AreEqual(0,directories.Count());

            directories = this.folder.GetDirectories("MockFiles/ChildDirectory");
            Assert.AreEqual(0, directories.Count());

            directories = this.folder.GetDirectories("MockFiles");
            Assert.AreEqual(2,directories.Count());

            directories = this.folder.GetDirectories("/MockFiles");
            Assert.AreEqual(2, directories.Count());

            directories = this.folder.GetDirectories("/MockFiles", "ChildDirectory");
            Assert.AreEqual(1, directories.Count());

            directories = this.folder.GetDirectories("/MockFiles", true);
            Assert.AreEqual(3, directories.Count());

            Assert.IsTrue(directories.Contains("/MockFiles/ChildDirectory"));
            Assert.IsTrue(directories.Contains("/MockFiles/ChildDirectory2"));
        }

        [TestMethod]
        [DeploymentItem("Core/FileSystems/MockFiles", "Directory1/MockFiles")]
        public void GetFiles()
        {
            //目录不存在时
            var files = this.folder.GetFiles("NotDirectory", false);
            Assert.AreEqual(0, files.Count());

            //指定的path为文件路径时
            files = this.folder.GetFiles("MockFiles/Mock1.txt", false);
            Assert.AreEqual(0, files.Count());

            //指定的路径为绝对路径时
            files = this.folder.GetFiles("/MockFiles", false);
            Assert.AreEqual(2, files.Count());

            //指定正确的路径，但是不级联查询
            files = this.folder.GetFiles("MockFiles", false);
            Assert.AreEqual(2,files.Count());

            //文件筛选
            files = this.folder.GetFiles("/MockFiles", "Mock2.txt");
            Assert.AreEqual(1,files.Count());

            //指定正确的多级路径，不级联查询
            files = this.folder.GetFiles("MockFiles/ChildDirectory", false);
            Assert.AreEqual(1, files.Count());

            //级联查询
            files = this.folder.GetFiles("MockFiles",true);
            Assert.AreEqual(5, files.Count());

            //返回文件路径
            Assert.IsTrue(files.Contains("MockFiles/Mock1.txt"));
            Assert.IsTrue(files.Contains("MockFiles/ChildDirectory/ChildMock1.txt"));

            //绝对路径格式
            files = this.folder.GetFiles("/MockFiles", true);
            Assert.IsTrue(files.Contains("/MockFiles/Mock1.txt"));
            Assert.IsTrue(files.Contains("/MockFiles/ChildDirectory/ChildMock1.txt"));
        }
    }
}
